import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

import 'package:get/get.dart';

import '../controllers/my_web_controller.dart';

class MyWebView extends GetView<MyWebController> {
  const MyWebView({super.key});
  @override
  Widget build(BuildContext context) {
    MyWebController controller = Get.put(MyWebController());
    return Scaffold(
      body: Obx(() => Stack(
            children: [
              controller.url.value == ""
                  ? const Text("")
                  : InAppWebView(
                      initialUrlRequest:
                          URLRequest(url: WebUri(controller.url.value)),
                      initialSettings: InAppWebViewSettings(
                          mediaPlaybackRequiresUserGesture: false,
                          javaScriptEnabled: true,
                          allowFileAccess: true),
                      onWebViewCreated: (controller) {
                        controller.addJavaScriptHandler(
                            handlerName: "getUserSettings",
                            callback: (arguments) {
                              print(arguments);
                              return null;
                            });
                      },
                      onLoadStart: (vc, url) {
                        controller.changeIsLoading();
                      },
                      onReceivedError: (vc, request, error) {
                        controller.changeIsLoading();
                      },
                      onPermissionRequest: (controller, request) async {
                        return PermissionResponse(
                            resources: request.resources,
                            action: PermissionResponseAction.GRANT);
                      }),
              controller.isLoading.value
                  ? const Center(
                      child: CircularProgressIndicator(
                        strokeWidth: 3,
                        color: Colors.blue, // 可以自定义颜色
                      ),
                    )
                  : const Text("")
            ],
          )),
    );
  }
}
